<template>
  <div>
    <main-box one="商品管理" two="商品列表">
      <el-button type="primary" @click="adddialogList">添加分类</el-button>
      <!-- 自定义树型控件 -->
      <table-tree
        class="treetable"
        :data="catelist"
        :columns="columns"
        show-index
        border
        index-text="#"
        :show-row-hover="false"
        :expand-type="false"
        :selection-type="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen;"></i>
          <i class="el-icon-error" style="color:red;" v-else></i>
        </template>
        <template slot-scope="scope" slot="order">
          <el-tag size="mini" v-if="scope.row.cat_level===0" type="success">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level===1" type="warning">二级</el-tag>
          <el-tag v-else size="mini" type="danger">三级</el-tag>
        </template>
        <template slot-scope="scope" slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </table-tree>
      <!-- 分页区域  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </main-box>
    <!-- 添加按钮弹出的对话框 -->
    <el-dialog title="添加列表" :visible.sync="AdddialogVisible" width="50%" @close="adddialogClose">
      <el-form
        :model="addruleForm"
        :rules="rules"
        ref="addruleFormref"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addruleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类 : ">
          <el-cascader
            v-model="selectedvalue"
            :options="parentCateList"
            :props="parentListProp"
            expand-trigger="hover"
            clearable
            change-on-select
            @change="addCateChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AdddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      catelist: [],
      // 获取商品分类数据列表
      // parentList:[],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          // 是否有效这一列比较特殊 固定的语法
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          // 模板的名字
          template: "isok"
        },
        {
          // 是否有效这一列比较特殊 固定的语法
          label: "排序",

          type: "template",
          // 模板的名字
          template: "order"
        },
        {
          // 是否有效这一列比较特殊 固定的语法 操作类
          label: "操作",

          type: "template",
          // 模板的名字
          template: "opt"
        }
      ],
      AdddialogVisible: false,
      //添加按钮表单双向绑定
      addruleForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      //   rules规则
      rules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
          // { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
        // 选中的是一个id  一级 二级 所以要用数组
      },
      parentCateList: [],
      parentListProp: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      selectedvalue: []
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取这个页面的所有信息
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error("查询分类失败");

      this.catelist = res.data.result;
      this.total = res.data.total;
      //   console.log(this.catelist);
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      // console.log(val);
      this.queryInfo.pagenum = val;
      this.getCateList();
    },
    adddialogList() {
      this.getParentList();
      this.AdddialogVisible = true;
    },
    async getParentList() {
      const { data: res } = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      // console.log(res);

      if (res.meta.status !== 200) return this.$message.error("查询失败");
      this.parentCateList = res.data;
      // console.log(this.parentCateList);
    },
    addCateChange() {
      //  就是选择框发生变化的时候会触发
      // console.log(this.selectedvalue);
      if (this.selectedvalue.length > 0) {
        //父级分类的id
        this.addruleForm.cat_pid = this.selectedvalue[
          this.selectedvalue.length - 1
        ];
        this.addruleForm.cat_level = this.selectedvalue.length;
        return;
      } else {
        this.addruleForm.cat_pid = 0;
        this.addruleForm.cat_level = 0;
      }
    },
    addCate() {
      // 确定按钮
      // console.log(this.addruleForm);
      this.$refs.addruleFormref.validate(async valid => {
        if (!valid) return;
        const {data:res}=await this.$http.post('categories',this.addruleForm)
        if(res.meta.status!==201) return this.$message.error('添加失败')
        this.$message.success('添加成功')
        this.getCateList();
      this.AdddialogVisible = false;
      });
    },
    adddialogClose() {
      // 关闭弹窗会触发的事件
      this.$refs.addruleFormref.resetFields();
      this.selectedvalue = [];
      this.addruleForm.cat_pid = 0;
      this.addruleForm.cat_level = 0;
    }
  }
};
</script> 
<style lang="less" >
.treetable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}

.el-cascader__dropdown {
  height: 400px;
  overflow: auto;
}
//
</style>